package com.system.shiro;

import java.util.List;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.staff.entity.Staff;
import com.staff.service.IStaffService;
import com.system.entity.Role;
import com.system.service.IPermissionService;
import com.system.service.IRoleService;

public class MyShiroRealm extends AuthorizingRealm
{
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IStaffService staffService;
    @Autowired
    private IRoleService roleService;
	
    //授权
    @Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		// TODO Auto-generated method stub
    	//获取登录时输入的用户名
        String staffNum=(String) principalCollection.fromRealm(getName()).iterator().next();
        System.out.println("login-username:"+staffNum);
        if(null!=staffNum) {        	
        	Set<String>listUrl=permissionService.getTheUrl(staffNum);//权限
		    Staff staff=staffService.findOne(staffNum);//用户信息
		    for (String p : listUrl) {
				System.out.println(staffNum+" 拥有的权限:"+p);
			}
		    SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
		    if(listUrl!=null&&!listUrl.isEmpty()){
		        for(String url:listUrl){
		            info.addStringPermission(url);//加入权限
		        }
		    }
		    List<Role> roleList=roleService.findByStaffId(staff.getId());
		    for (Role role : roleList) {
				System.out.println(staffNum+" 拥有的角色:"+role.getName());
			}
		    if(roleList!=null&&!roleList.isEmpty()){
		        for(Role role:roleList){
		            info.addRole(role.getName());//加入角色
		        }
		    }
		    return info;
		}
		return null;
	}

	//身份验证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		//UsernamePasswordToken token=(UsernamePasswordToken)token;
        //通过表单接收的用户名
    	//首先从缓存中获取记录
        //Cache<Object, AuthenticationInfo> info = getAuthenticationCache();
      
        String staffNum = (String)token.getPrincipal();  //得到用户名
        String password = new String((char[])token.getCredentials()); //得到密码
        
        System.out.println(staffNum+"--"+password);
        if(staffNum!=null&&!"".equals(staffNum)){
            Staff staff=staffService.findOne(staffNum);
            System.out.println("staff(username)："+staffNum);
            if(staff!=null&&password.equals(staff.getPassword())){
                return new SimpleAuthenticationInfo(staff.getStaffNum(),staff.getPassword(),getName());
            }
        }
		return null;
	}

}
